package cn.zhaoyuening.zshop4j.portal.web;

import cn.zhaoyuening.zshop4j.common.utils.JsonUtils;
import cn.zhaoyuening.zshop4j.content.service.ContentService;
import cn.zhaoyuening.zshop4j.manager.pojo.TbContent;
import cn.zhaoyuening.zshop4j.portal.bean.AD1Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zhao on 2017/4/12.
 */
@Controller
public class IndexController {

    //首页翻滚大图信息
    @Value("${AD1_ID_BIG}")
    private Long AD1_ID_BIG;
    @Value("${AD1_WIDTH}")
    private Integer AD1_WIDTH ;
    @Value("${AD1_WIDTH_B}")
    private Integer AD1_WIDTH_B ;
    @Value("${AD1_HEIGHT}")
    private Integer AD1_HEIGHT;
    @Value("${AD1_HEIGHT_B}")
    private Integer AD1_HEIGHT_B;

    @Autowired
    private ContentService contentService;


    @RequestMapping("index")
    public String showIndex(Model model) {
        //首页滚动大图
        List<TbContent> list = contentService.findByCategoryId(AD1_ID_BIG);
        List<AD1Bean> ad1BeanList = content2AD1BeanList(list, AD1_HEIGHT, AD1_HEIGHT_B, AD1_WIDTH, AD1_WIDTH_B);
        String jsonData = JsonUtils.objectToJson(ad1BeanList);
        model.addAttribute("ad1", jsonData);
        return "index";
    }


    /**
     * content list 转换为 ad1bean list
     * @param contentList
     * @param height
     * @param height2
     * @param width
     * @param width2
     * @return
     */
    private List<AD1Bean> content2AD1BeanList(List<TbContent> contentList,int height,int height2,int width,int width2) {
        List<AD1Bean> ad1BeanList = new ArrayList<>();

        for (TbContent content :
                contentList) {
            AD1Bean ad1bean = new AD1Bean();
            ad1bean.setAlt(content.getTitle());
            ad1bean.setSrc(content.getPic());
            ad1bean.setSrcB(content.getPic2());
            ad1bean.setHref(content.getUrl());
            ad1bean.setHeight(height);
            ad1bean.setHeightB(height2);
            ad1bean.setWidth(width);
            ad1bean.setWidthB(width2);
            ad1BeanList.add(ad1bean);
        }

        return ad1BeanList;
    }



}
